package com.zuo.mall.controller;

import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.common.api.CommonResult;
import com.zuo.mall.dto.PmsBrandParam;
import com.zuo.mall.dto.PmsProductParam;
import com.zuo.mall.entity.PmsBrand;
import com.zuo.mall.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.junit.jupiter.params.shadow.com.univocity.parsers.annotations.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("品牌管理")
@RestController
@RequestMapping("/brand")
public class PmsBrandController {
    @Autowired
    private PmsBrandService brandService;

    @GetMapping("list")
    public CommonResult getList(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "showStatus", required = false) Integer showStatus,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize
    ) {
        CommonPage<PmsBrand> page = brandService.listBrand(keyword, showStatus, pageNum, pageSize);
        return CommonResult.success(page);
    }

    @PostMapping("create")
    public CommonResult create(@RequestBody @Validated PmsBrandParam brandParam) {
        int count = brandService.create(brandParam);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改前回显")
    @GetMapping("{id}")
    public CommonResult<PmsBrand> getItem(@PathVariable("id") Long id) {
        PmsBrand byId = brandService.getById(id);
        return CommonResult.success(byId);
    }

    @ApiOperation("批量修改品牌制造商信息")
    @PostMapping("/update/factoryStatus")
    public CommonResult updateFactoryStatus(@RequestParam List<Long> ids, @RequestParam Integer factoryStatus) {
        int count = brandService.updateFactoryStatusBatch(ids, factoryStatus);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("批量修改该是否显示信息")
    @PostMapping("update/showStatus")
    public CommonResult updateShowStatus(@RequestParam List<Long> ids, @RequestParam Integer showStatus) {
        int count = brandService.update(ids, showStatus);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("删除品牌")
    @GetMapping("delete/{brandId}")
    public CommonResult delete(@PathVariable("brandId") Long brandId) {
        int count = brandService.delete(brandId);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
